package com.huanghuai.retail.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huanghuai.retail.dto.StoreCheckDto;
import com.huanghuai.retail.entity.SCheck;
import com.huanghuai.retail.entity.SCheckDetail;
import com.huanghuai.retail.mapper.StoreCheckMapper;
import com.huanghuai.retail.service.StoreCheckDetailService;
import com.huanghuai.retail.service.StoreCheckService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class StoreCheckServiceImpl extends ServiceImpl<StoreCheckMapper, SCheck> implements StoreCheckService {
    @Resource
    private StoreCheckDetailService storeCheckDetailService;
    @Override
    @Transactional
    public boolean saveCheckAndDetails(StoreCheckDto storeCheckDto) {
        log.info("传过来的盘点数据为：{}",storeCheckDto);
        storeCheckDto.setDate(new Date());
        boolean res1 = this.save(storeCheckDto);    // 在s_check里面插入数据，此时id已经被设置
        List<SCheckDetail> details = storeCheckDto.getDetails();
        List<SCheckDetail> resDetail = details.stream().map((item)->{
           item.setCheckId(storeCheckDto.getId());
           return item;
        }).collect(Collectors.toList());
        boolean res2 = storeCheckDetailService.saveBatch(resDetail);

        return res1 && res2;
    }
}
